var MyTable={
    searchForm:"searchForm",
    searchUrl:"/wxmaterial/list",
	tableId:"lgTable",
	table:null,
	prefix:"/wxmaterial"
};
var total=0;
var offset=0;
var limit=14;
var laypage;
$(function() {
	     reLoad();
	     toPage();
});

function reLoad() {
	 var config={
				url : MyTable.searchUrl,
				data:{"offset":offset,"limit":limit,"type":types},
				async :false
		}
		EasyAjax.post_json(config,function(r){
			total=r.total;
			appendStr(r.rows);
			 $('.file-box').each(function () {
	                animationHover(this, 'pulse');
	         });
		}); 
}

function toPage(){
	  layui.use('laypage', function(){
    	  laypage = layui.laypage;
    	  layPage();
    	});
}

function layPage(){
	laypage.render({
	    elem: 'test1' //注意，这里的 test1 是 ID，不用加 # 号
	    ,count: total //数据总数，从服务端得到
	    ,limit:limit
	    ,jump: function(obj, first){
	    	 if(!first){ //一定要加此判断，否则初始时会无限刷新
	    		 offset=(obj.curr-1)*obj.limit;
	    		 reLoad();//一定要把翻页的ajax请求放到这里，不然会请求两次。
			 }
	    	
	     }
	  });
}

function appendStr(rows){
	var str="";
	$("#files").empty();
	$.each(rows,function(i,item){
		str+="<div class='file-box'>";
		str+="<div class='file'>";
		str+="<a href='file_manager.html#'>";
		str+="<span class='corner'></span>";
		switch (item.type) {
		case "image":
			str+="<div class='image' onclick='chooseMedia("+JSON.stringify(item)+")' >";
			str+="<img alt='image' class='img-responsive' src='"+item.localUrl+"'>";
			str+="</div>";	
			break;
		case "video":
		    str+="<div class='icon' onclick='chooseMedia("+JSON.stringify(item)+")' >";
			//str+="<i class='img-responsive fa fa-film'></i>";
			str+="<video src='"+item.localUrl+"' controls='controls'></video>";
			str+="</div>";
				break;
		case "voice":
			str+="<div class='icon' onclick='chooseMedia("+JSON.stringify(item)+")' >" ;
			//str+="<i class='fa fa-music'></i>" ;
			str+="<audio style='width: 100%;' src="+item.localUrl+" controls='controls'</audio>";
			str+="</div> " ;
		break;
		default:
			break;
		}
		str+="<div class='file-name'>";
		if(item.filename){
			  str+=(item.filename.length>5?item.filename.substring(0,5):item.filename);
		}
		str+="<br/>";
		str+="<small>添加时间："+item.gmtCreate+"</small>";
		str+="</div>";
		
		str+="</a>";
		str+="</div>";
		str+="</div>";
	})
	$("#files").append(str);
}

function chooseMedia(obj){
	window.parent[0].getMedia(obj);
	var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
	parent.layer.close(index);
}

function checkSelect(){
	var rows = $('#'+MyTable.tableId).bootstrapTable('getSelections'); // 返回所有选择的行，当没有选择的记录时，返回一个空数组
	if (rows.length == 0) {
		layer.msg("请选择要删除的数据");
		return null;
	}else{
		var ids = new Array();
		$.each(rows, function(i, row) {
			ids[i] = row.userId;
		});
		return ids;
	}
}
